<?php

/**
 * 设备管理状态更新
 */
class Action
{
    private $per_page = 100;

    /**
     * 显示列表
     */
    public function index() {
        Response::display('admin/import_device.html');
    }

    /**
     * 修改状态
     */
    public function import_device() {
        $renew_type = Request::getParam('device_type', 0);
        $device_ids = Request::getParam('device_ids', '');
        if ($device_ids) {
            $device_list = explode(PHP_EOL, $device_ids);
            foreach ($device_list as $k => $v) {
                $device_info = explode(',', $v);
                $code = trim($device_info[0]);
                $type = device_helper::get_device_type_by_code($code);
                $info = trim($device_info[1]) ? trim($device_info[1]) : '';
                if ($code && $type) {
                    $code = strtoupper($code);
                    $device = _model('device')->read(array('code' => $code));
                    if ($device) {
                        _model('device')->update(array('code' => $code), array(
                            'type' => $type,
                            'status' => $renew_type
                        ));
                        $dev_id = $device['id'];
                    } else {
                        if ($type == 'ZX' && substr($code, 0, 3) == 'ZX1') {
                            $dev_id = _model('device')->create(array(
                                'code' => $code,
                                'type' => $type,
                                'status' => $renew_type,
                                'add_time' => datetime(),
                            ));
                        }
                    }
                    if ($dev_id) {
                        device_helper::save_user_device_record('', $type, $dev_id, $renew_type, '', $info);
                    }
                }
            }

            Response::redirect(AnUrl('device/admin/index'));
        } else {
            return '请选择文件';
        }
    }
}
